package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.QuoteItem;
import com.qianying.service.QuoteItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 报价项目控制器
 * 提供报价项目的HTTP接口，处理前端请求
 */
@RestController
@RequestMapping("/admin/quoteItem")
@Tag(name = "报价项目管理", description = "报价项目信息的增删改查接口")
public class QuoteItemController {

    @Autowired
    private QuoteItemService quoteItemService;

    /**
     * 分页查询报价项目列表
     * @param current 页码
     * @param size 每页条数
     * @param itemName 报价项目名称（模糊查询）
     * @return 分页结果
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询报价项目列表",
            description = "支持按报价项目名称模糊查询，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码", example = "1"),
                    @Parameter(name = "size", description = "每页条数", example = "10"),
                    @Parameter(name = "itemName", description = "报价项目名称（模糊查询）", example = "人工费")
            }
    )
    public CommonResult<IPage<QuoteItem>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String itemName) {
        Page<QuoteItem> page = new Page<>(current, size);
        return quoteItemService.getPage(page, itemName);
    }

    /**
     * 根据ID查询报价项目详情
     * @param id 主键ID
     * @return 报价项目详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询报价项目详情",
            description = "通过ID获取报价项目的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<QuoteItem> getById(@RequestParam Long id) {
        return quoteItemService.getById(id);
    }

    /**
     * 新增报价项目
     * @param quoteItem 报价项目信息（ID需为空）
     * @return 操作结果
     */
    @PostMapping("/saveItem")
    @Operation(
            summary = "新增报价项目",
            description = "创建新的报价项目记录，报价项目名称为必填项，ID必须为空",
            parameters = {
                    @Parameter(name = "itemName", description = "报价项目名称", required = true, example = "人工费"),
                    @Parameter(name = "itemCode", description = "报价项目代号", example = "LABOR"),
                    @Parameter(name = "description", description = "报价项目说明", example = "人工费用，按工时计算")
            }
    )
    public CommonResult<?> saveItem(@RequestBody QuoteItem quoteItem) {
        return quoteItemService.saveItem(quoteItem);
    }

    /**
     * 更新报价项目
     * @param quoteItem 报价项目信息（需包含ID）
     * @return 操作结果
     */
    @PostMapping("/updateItem")
    @Operation(
            summary = "更新报价项目",
            description = "修改已有报价项目信息，必须包含ID和报价项目名称",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "itemName", description = "报价项目名称", required = true, example = "人工费"),
                    @Parameter(name = "itemCode", description = "报价项目代号", example = "LABOR"),
                    @Parameter(name = "description", description = "报价项目说明", example = "人工费用，按工时计算")
            }
    )
    public CommonResult<?> updateItem(@RequestBody QuoteItem quoteItem) {
        return quoteItemService.updateItem(quoteItem);
    }

    /**
     * 根据ID删除报价项目
     * @param id 主键ID
     * @return 操作结果
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除报价项目",
            description = "通过ID删除指定的报价项目记录",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return quoteItemService.removeById(id);
    }

    /**
     * 查询所有报价项目（不分页）
     * @return 报价项目列表
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有报价项目",
            description = "返回所有报价项目列表，适用于下拉选择等场景"
    )
    public CommonResult<List<QuoteItem>> getAll() {
        return quoteItemService.getAll();
    }
}